package dao

import (
	"gorm.io/gorm"
	"gotham-plan-manage/internal/infra/database/mysql/plan/po"
)

/*
   @author:zhongyang
   @date:2023/4/5
   @description:问题集数据库操作
*/

type QuestionDao struct {
}

func NewQuestionDao() *QuestionDao {
	return &QuestionDao{}
}

func (q *QuestionDao) DeleteByPlanId(db *gorm.DB, questionPO *po.QuestionPO) error {
	return db.Where("plan_id", questionPO.PlanId).Delete(&questionPO).Error
}

func (q *QuestionDao) FindOneDayQuestions(db *gorm.DB, planId string, taskDate string, userId string) ([]*po.QuestionPO, error) {
	var res []*po.QuestionPO
	// 根据日期和计划ID确认问题集
	err := db.Where("user_id = ? and plan_id = ? and todo_task_date = ?", userId, planId, taskDate).Find(&res).Error
	if err != nil {
		return nil, err
	}
	return res, nil
}

func (q *QuestionDao) SearchQuestions(db *gorm.DB, userID string, planId string, content string) ([]*po.QuestionPO, error) {
	var res []*po.QuestionPO
	tx := db.Where("plan_id = ?", planId)
	if content != "" {
		tx = tx.Where("question like ?", "%"+content+"%")
	}
	err := tx.Find(&res).Error
	return res, err
}

func (q *QuestionDao) BatchDelete(db *gorm.DB, questionsPOs []*po.QuestionPO) error {
	return db.Delete(&questionsPOs).Error
}

func (q *QuestionDao) Save(db *gorm.DB, questionPOs []*po.QuestionPO) error {
	return db.Save(questionPOs).Error
}
